#import "NSString+Tools.h"
@implementation NSString (Tools)
- (BOOL)isHexString {
    if (self == nil || [self isKindOfClass:[NSNull class]]) {
        return NO;
    }
    NSString *hexRegex = @"^(0x)?([0-9A-Fa-f]+)$";
    NSString *lowerStr = [self lowercaseString];
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", hexRegex];
    return [regexTest evaluateWithObject:lowerStr];
}
+ (BOOL)isEmpty:(NSString *)string {
    return ((string) == nil || (string) == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [(string) isEqual:@""] || [(string) isEqual:@" "]);
}
-(NSString *)trim
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
+ (NSString *)_859ToUTF8:(NSString *)oldStr
{
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin1);
    return [NSString stringWithUTF8String:[oldStr cStringUsingEncoding:enc]];
}
#pragma mark -- text color
+ (NSMutableAttributedString *)setAllText:(NSString *)allStr andSpcifiStr:(NSString *)keyWords withColor:(UIColor *)color specifiStrFont:(UIFont *)font{
    if (keyWords == nil) {
        return nil;
    }
    NSMutableAttributedString *mutableAttributedStr = [[NSMutableAttributedString alloc] initWithString:allStr];
    if (color == nil) {
        color = [UIColor redColor];
    }
    if (font == nil) {
        font = [UIFont systemFontOfSize:17];
    }
    for (NSInteger j=0; j<=keyWords.length-1; j++) {
        NSRange searchRange = NSMakeRange(0, [allStr length]);
        NSRange range;
        NSString *singleStr = [keyWords substringWithRange:NSMakeRange(j, 1)];
        while
            ((range = [allStr rangeOfString:singleStr options:NSLiteralSearch range:searchRange]).location != NSNotFound) {
                searchRange = NSMakeRange(NSMaxRange(range), [allStr length] - NSMaxRange(range));
                [mutableAttributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
                [mutableAttributedStr addAttribute:NSFontAttributeName value:font range:range];
            }
    }
    return mutableAttributedStr;
}
+ (NSString *)currentSystemLanguage {
    NSString *sysLang = [NSLocale preferredLanguages].firstObject;
    if ([sysLang containsString:@"en"]) {       
        if ([sysLang isEqualToString:@"en-AU"] || [sysLang isEqualToString:@"en-CA"] || [sysLang isEqualToString:@"en-GB"] || [sysLang isEqualToString:@"en-IN"] || [sysLang isEqualToString:@"en-NZ"] || [sysLang isEqualToString:@"en-US"]) {
            return sysLang;
        } else {return @"en-US";}
    } else if ([sysLang containsString:@"fr"]) { 
        return @"fr-FR";
    } else if ([sysLang containsString:@"de"]) { 
        return @"de-DE";
    } else if ([sysLang containsString:@"es"]) { 
        return @"es-ES";
    } else if ([sysLang containsString:@"da"]) { 
        return @"da-DK";
    } else if ([sysLang containsString:@"it"]) { 
        return @"it-IT";
    } else if ([sysLang containsString:@"fi"]) { 
        return @"fi-FI";
    } else if ([sysLang containsString:@"ca"]) { 
        return @"ca-ES";
    } else if ([sysLang containsString:@"ar"]) { 
        return @"ar-EG";
    } else if ([sysLang containsString:@"ja"]) { 
        return @"ja-JP";
    } else if ([sysLang containsString:@"hi"]) { 
        return @"hi-IN";
    } else if ([sysLang containsString:@"hi"]) { 
        return @"hi-IN";
    } else if ([sysLang containsString:@"ko"]) { 
        return @"ko-KR";
    } else if ([sysLang containsString:@"nb"]) { 
        return @"nb-NO";
    } else if ([sysLang containsString:@"nl"]) { 
        return @"nl-NL";
    } else if ([sysLang containsString:@"pl"]) { 
        return @"pl-PL";
    } else if ([sysLang containsString:@"pt"]) { 
        return @"pt-PT";
    } else if ([sysLang containsString:@"ru"]) { 
        return @"ru-RU";
    } else if ([sysLang containsString:@"sv"]) { 
        return @"sv-SE";
    } else if ([sysLang containsString:@"th"]) { 
        return @"th-TH";
    }
    else {
        return sysLang;
    }
}
- (BOOL)pk_isNotEmpty {
    NSCharacterSet *blank = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    for (NSInteger i = 0; i < self.length; ++i) {
        unichar c = [self characterAtIndex:i];
        if (![blank characterIsMember:c]) {
            return YES;
        }
    }
    return NO;
}
- (NSString *)pk_deleteFirstCharacter {
    return [self pk_backSubstringFromIndex:1];
}
- (NSString *)pk_backSubstringFromIndex:(NSUInteger)fromIndex {
    if (![self pk_isNotEmpty]) {
        return @"";
    }
    if (fromIndex > self.length - 1) {
        return @"";
    }
    return [self substringFromIndex:fromIndex];
}
- (NSString *)pk_deleteLastCharacter {
    return [self pk_frontSubstringToIndex:self.length - 1];
}
- (NSString *)pk_frontSubstringToIndex:(NSUInteger)toIndex {
    if (![self pk_isNotEmpty]) {
        return @"";
    }
    if (toIndex > self.length - 1) {
        return @"";
    }
    return  [self substringToIndex:toIndex];
}
+ (NSString *)pk_stringValueCurrency:(NSNumber *)number {
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    formatter.numberStyle = NSNumberFormatterCurrencyStyle;
    formatter.maximumFractionDigits = 2;
    formatter.minimumFractionDigits = 0;
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    NSMutableString *muds = [formatter stringFromNumber:number].mutableCopy;
    return [muds pk_deleteFirstCharacter];
}
- (NSString *)cleanPhoneNumber {
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\D" options:0 error:&error];
    NSString *cleanedNumber = [regex stringByReplacingMatchesInString:self
                                                              options:0
                                                                range:NSMakeRange(0, [self length])
                                                         withTemplate:@""];
    return cleanedNumber;
}
+ (NSString *)ct_encodeUrl:(NSString *)urlString {
    NSString *str = [NSString stringWithFormat:@"U%@M", urlString];
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64Str = [data base64EncodedStringWithOptions:0];
    urlString = [NSString stringWithFormat:@"%@%@", @"hQhyZ", base64Str];
    urlString = [urlString stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
    urlString = [urlString stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"="]];
    return urlString;
}
+ (NSString *)ct_decodeUrl:(NSString *)urlString {
    NSString *domain = [CTEnvironmentManager shareManager].environmentUrl;
    if ([urlString containsString:domain]) {
        urlString = [urlString substringFromIndex:domain.length];
    }
    NSString *base64Str = [urlString substringFromIndex:5];
    NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString *urlStr = [str stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];
    urlStr = [urlStr stringByReplacingCharactersInRange:NSMakeRange(urlStr.length - 1, 1) withString:@""];
    return urlStr;
}
+ (NSString *)stringReplacedWithLocalizedString:(NSString *)localizeddString source:(NSString *)source target:(NSString *)target {
    NSString *formatedMessage = @"";
    formatedMessage = [NSString stringWithFormat:localizeddString,source];
    return formatedMessage;
}
@end
